package cn.stylefeng.guns.modular.shi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.shi.entity.SRecommend;
import cn.stylefeng.guns.modular.shi.model.params.SRecommendParam;
import cn.stylefeng.guns.modular.shi.service.SRecommendService;
import cn.stylefeng.guns.modular.wxuser.service.TaoWxUserService;
import cn.stylefeng.guns.sys.modular.tao.entity.Goods;
import cn.stylefeng.guns.sys.modular.tao.entity.GoodsPic;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsPicService;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * 控制器
 * 商品种草表
 * @author 石博士
 * @Date 2019-11-15 11:08:39
 */
@Controller
@RequestMapping("/sRecommend")
public class SRecommendController extends BaseController {

    private String PREFIX = "/sRecommend";

    /**
     * 商品种草表
     */
    @Autowired
    private SRecommendService sRecommendService;


    /**
     * 商品表
     */
    @Autowired
    private GoodsService goodsService;


    /**
     * 微信用户表
     */
    @Autowired
    private TaoWxUserService taoWxUserService;


    /**
     * 商品图片表
     */
    @Autowired
    private GoodsPicService goodsPicService;


    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/sRecommend.html";
    }


    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/sRecommend_add.html";
    }


    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/sRecommend_edit.html";
    }


    /**
     * 商品种草表
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(SRecommendParam sRecommendParam) {
        Goods Goods = goodsService.getByIds(sRecommendParam.getId());
        sRecommendParam.setNameId(Goods.getId().toString());
        sRecommendParam.setNames(Goods.getFullname());
        sRecommendParam.setAddingTime(new Date());
        try {
            GoodsPic pic = goodsPicService.getByGoodsId(String.valueOf(sRecommendParam.getId()));
            sRecommendParam.setPicturesUrl(pic.getGoodsPics());
        } catch (NullPointerException e) {
            System.out.println("发生异常的原因为 :"+e.getMessage());
        }
        sRecommendParam.setOpenid(null);
        sRecommendParam.setDzTotal(Long.parseLong("0"));
        sRecommendParam.setEvaluationQuantity(Long.parseLong("0"));
        sRecommendParam.setContents(Goods.getComment());
        this.sRecommendService.add(sRecommendParam);
        return ResponseData.success();
    }


    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(SRecommendParam sRecommendParam) {
        this.sRecommendService.update(sRecommendParam);
        return ResponseData.success();
    }


    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SRecommendParam sRecommendParam) {
        this.sRecommendService.delete(sRecommendParam);
        return ResponseData.success();
    }


    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SRecommendParam sRecommendParam) {
        SRecommend detail = this.sRecommendService.getById(sRecommendParam.getId());
        return ResponseData.success(detail);
    }


    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(SRecommendParam sRecommendParam) {
        LayuiPageInfo pageBySpec = this.sRecommendService.findPageBySpec(sRecommendParam);
        QueryWrapper<SRecommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("adding_time");
        List<SRecommend> list = sRecommendService.list(queryWrapper);
        pageBySpec.setData(list);
        return pageBySpec;
    }








}


